<template>
    <div >
    
  <el-table
    :data="pagedTableData"
    style="width: 100%">
    <el-table-column v-for="(item) in Object.keys(tableHeadData)" :key="item.bishe_id"
      :label="tableHeadData[item]"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{  scope.row[item]  }}</span>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="detail(scope.row)">详情</el-button>
        <el-button
          class="my"
          size="mini"
          
          @click="choose(scope.$index, scope.row)">选择</el-button>
      </template>
    </el-table-column>
  </el-table>
    
  <div class="my-paging">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="computedTotal">
    </el-pagination> 
  </div>
  
</div>
</template>

<script>
import axios from 'axios';




export default {
    data(){
        return{
            identity: 'student',
            id: 0,

            //表头数据
            tableHeadData:{
                    bisheId: '毕设编号',
                    teacherId : '老师编号',
                    bisheName: '毕设标题',
                    univercity: '所属学院',
                    major: '所属专业',
                    
                    createtime: '课设上传时间',
                    teacherName: '老师姓名',
        
                    islock: '最终学生编号/已双向选择'
            },

            //编辑数据
            message: [],

            labelPosition: 'right',
            formLabelAlign: {
              name: '',
              region: '',
              type: ''
            },
            tableData: [],
          currentPage: 1,
          pageSize: 10,
          

        }
    },
    computed:{
      computedTotal(){
        return this.tableData.length
      },
      pagedTableData() {
        if (Array.isArray(this.tableData) && this.tableData.length > 0) {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return this.tableData.slice(startIndex, endIndex);
        }else{
          return []
        }
      }
    },
    components:{
        

    },
    props:[],
    methods:{
        detail(row) {
            
            this.$router.push({path: `/student/bisheInfo/DetailPage/${row.bishe_id}`,query:{bisheData:row}})
        },
        choose(index, row) {
          this.$confirm('学生只能选择一个毕设,是否确定？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(action => {
            if(action === 'confirm'){
              axios.post(`/prod-api/xuanze/add`,{bisheId:row.bisheId,teacherId:row.teacherId,studentId:this.id})
                    .then(response=>{
                        if(response.data.code === 0){
                          this.$message({
                              type: 'success',
                              message: '选择成功!'
                            })
                        }
                    })

              

            }
            // 接口传递信息是否选择成功，根据毕设是否被锁定来判断

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
            console.log(index,row);
            
            console.log(this.pagedTableData);
        },
        handleSizeChange(val) {
          this.pageSize = val;
        },
        handleCurrentChange(val) {
          this.currentPage = val;
        }
    },
    created(){
      this.id = JSON.parse(localStorage.getItem('data')).studentId

      axios.post(`/prod-api/bishe/getIspssBishe`,{ispass:'通过'})
            .then(response=>{
              if(response.data.code === 0)
              console.log(response.data);
                this.tableData = response.data
                this.tableData = this.tableData.filter(element=>element.islock === 0)
            }).catch(error =>{
              console.log(error);
            })
    
  
}
}

</script>

<style>
.my-paging{
  text-align: center;
  margin-top: 20px;
}
.my{
  margin-left: 0px !important;
}
</style>